#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        ObjectManager.cs
// Reason:                           Wrapper to contain and manage all game objects
//////////////////////////////////////////////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Base;

namespace Manager
{
    public sealed class ObjectManager
    {

        /////////////////////////////////////
        //List of all the objects
        ////////////////////////////////////
        private List< CBase> m_lObjectList = null;

        
        /////////////////////////////////////
        // Make this class a singleton
        ////////////////////////////////////
        private static ObjectManager m_instance = new ObjectManager();


        /// <Accesors / Mutators>
        /// Function:		Accesors / Mutators
        ///
        /// Purpose:		to get the specified type. / to set the specified type.
        /// </Accesors / Mutators>
        public static ObjectManager Instance
        {
            get
            {
                return m_instance;

            }  
       }

        ///<Public Functions>
        /// Function:		Public Functions
        ///
        /// Purpose:		public funtions that can be called
        ///</Public Functions>

        private ObjectManager()
        {
           // m_instance = new ObjectManager();
            m_lObjectList = new List<CBase>();
        }

        public void UpdateObjects(float fElapsedTime)
        {
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                m_lObjectList[i].Update(fElapsedTime);
            }
        }
        public void UpdateInput()
        {
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                m_lObjectList[i].Input();
            }
        }
        public void RenderObjects()
        {
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                m_lObjectList[i].Render();
            }
        }
        public void RemoveObject(CBase pObject)
        {
            //Check for a vaild CBase *
            if (pObject == null)
                return;
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                if (m_lObjectList[i] == pObject)
                {
                     m_lObjectList[i].Release();
                    // Remove the object from the list
                    m_lObjectList.Remove(m_lObjectList[i]);
                    break;
                }
            }
        }
        public void AddObject(CBase pObject)
        {
            if (pObject == null)
                return;
            m_lObjectList.Add(pObject);
           
        }
        public void RemoveAllObjects()
        {
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                m_lObjectList[i].Release();
                
                m_lObjectList.Remove(m_lObjectList[i]);
            }
        }
        public void CheckCollision()
        {
            for (int i = 0; i < m_lObjectList.Count; ++i)
            {
                for (int j = 0; j < m_lObjectList.Count; ++j)
                {
                    if (m_lObjectList[i].Type == m_lObjectList[j].Type)
                        continue;
                    else
                    {
                        if (m_lObjectList[i].CheckCollision(m_lObjectList[j]))
                            break;
                    }
                }
            }
        }
    }
}